package com.gdut.simple.common;

import android.content.Context;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class CityUtils {

    private static final String RESOURCE_PATH = "assets/city.json";

    /**
     * 获取省份列表
     */
    public static List<String> getProvinces(Context context) {
        try {
            InputStream in = context.getClass().getClassLoader().getResourceAsStream(RESOURCE_PATH);
            if (in != null) {
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                    sb.append("\r\n");
                }
                br.close();
                String resource = sb.toString();
                List<String> provinces = new ArrayList<>();
                JSONObject object = new JSONObject(resource);
                JSONArray array = object.getJSONArray("p");
                for (int i = 0; i < array.length(); i++) {
                    provinces.add(array.optString(i));
                }
                return provinces;
            }
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据省份获取城市列表
     */
    public static List<String> getCities(Context context, String province) {
        try {
            InputStream in = context.getClass().getClassLoader().getResourceAsStream(RESOURCE_PATH);
            if (in != null) {
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                    sb.append("\r\n");
                }
                br.close();
                String resource = sb.toString();
                List<String> cities = new ArrayList<>();
                JSONObject object = new JSONObject(resource);
                JSONObject provinces = object.getJSONObject("c");
                JSONArray cityArray = provinces.getJSONArray(province);
                for (int i = 0; i < cityArray.length(); i++) {
                    cities.add(cityArray.optString(i));
                }
                return cities;
            }
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取数据版本
     */
    public static int getVersion(Context context) {
        try {
            InputStream in = context.getClass().getClassLoader().getResourceAsStream(RESOURCE_PATH);
            if (in != null) {
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                    sb.append("\r\n");
                }
                br.close();
                JSONObject object = new JSONObject(sb.toString());
                return object.getInt("v");
            }
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return -1;
    }

}
